<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="工单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['no']" placeholder="请输入工单号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="处方号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['outNo']" placeholder="请输入处方号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="患者姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['patientName']" placeholder="请输入患者姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="0:女; 1:男; 2：未知" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['patientGender']" placeholder="请输入0:女; 1:男; 2：未知" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="患者年龄" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['patientAge']" placeholder="请输入患者年龄"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="daijian:代煎; daipei:自煎" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['workCate']" placeholder="请输入daijian:代煎; daipei:自煎"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="剂数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['paster']" placeholder="请输入剂数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="每剂袋数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['packet']" placeholder="请输入每剂袋数" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="科室" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['hospitalOffice']" placeholder="请输入科室"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="医生" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['doctor']" placeholder="请输入医生"></a-input>
            </a-form-item>
          </a-col>
          </a-col>
          </a-col>
          <a-col :span="24">
            <a-form-item label="nner:院内自取; shunfeng:顺丰快递; jingdong:京东快递; " :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['expressType']" placeholder="请输入nner:院内自取; shunfeng:顺丰快递; jingdong:京东快递; "></a-input>
            </a-form-item>
          </a-col>

          </a-col>
          <a-col :span="24">
            <a-form-item label="医院" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['hospital']" placeholder="请输入医院"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['idCard']" placeholder="请输入身份证号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="用药方式，口服-koufu，外-waiyong，其他-qita" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['medicineType']" placeholder="请输入用药方式，口服-koufu，外-waiyong，其他-qita"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="剂量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['dose']" placeholder="请输入剂量" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="每日几剂" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['dayOfTimes']" placeholder="请输入每日几剂" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="每袋ml" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['unitMl']" placeholder="请输入每袋ml" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="服用量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['takingNumber']" placeholder="请输入服用量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="每剂重量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['eachWeight']" placeholder="请输入每剂重量" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="快递编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['courierNumber']" placeholder="请输入快递编号"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="支付状态0未支付，1已支付" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['status']" placeholder="请输入支付状态0未支付，1已支付" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['amount']" placeholder="请输入金额" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="快递名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['courierName']" placeholder="请输入快递名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="付款方式，寄付现结、货到付款" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['courierType']" placeholder="请输入付款方式，寄付现结、货到付款"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="预估价格" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['forecastAmount']" placeholder="请输入预估价格" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="总重量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['weight']" placeholder="请输入总重量"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="件数" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['count']" placeholder="请输入件数" style="width: 100%"/>
            </a-form-item>
          </a-col>
              <a-col :span="24">
          <a-form-item label="收件人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="['expressName']" placeholder="请输入收件人姓名"></a-input>
          </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="收货人电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['expressPhone']" placeholder="请输入收货人电话"></a-input>
            </a-form-item>
            </a-col>
            <a-col :span="24">
            <a-form-item label="收件地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['expressAddress']" placeholder="请输入收件地址"></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  

  export default {
    name: 'OutOrderForm',
    components: {
      JFormContainer,
      JDate,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
validatorRules: {
},
        url: {
          add: "/outOrder/outOrder/add",
          edit: "/outOrder/outOrder/edit",
          queryById: "/outOrder/outOrder/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'no','outNo','patientName','patientGender','patientAge','workCate','paster','packet','hospitalOffice','doctor','doctorId','expressId','expressType','expressName','hospital','hospitalId','idCard','medicineType','dose','dayOfTimes','unitMl','takingNumber','eachWeight','courierNumber','status','amount','forecastTime','lineCount','courierName','courierType','forecastAmount','startPoint','endPoint','weight','count','payName','appointmentProcess','appointmentNumber','appointmentStatus','submitTime','expressPhone','expressAddress'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'no','outNo','patientName','patientGender','patientAge','workCate','paster','packet','hospitalOffice','doctor','doctorId','expressId','expressType','expressName','hospital','hospitalId','idCard','medicineType','dose','dayOfTimes','unitMl','takingNumber','eachWeight','courierNumber','status','amount','forecastTime','lineCount','courierName','courierType','forecastAmount','startPoint','endPoint','weight','count','payName','appointmentProcess','appointmentNumber','appointmentStatus','submitTime'))
      },
    }
  }
</script>